#encoding=utf-8
from utils.ext import inner_db
from marshmallow import Schema, fields, ValidationError, validate, validates_schema
import datetime

class RecordsQuerySchema(Schema):
    account = fields.String(required=True)
    query_interval = fields.Integer(missing=7)
    page_index = fields.Integer(missing=1)  ## 指定展示的页数，默认展示第一页
    per_page_items = fields.Integer(missing=10)  ## 每一页展示的行数

class RecordQuerySchema(Schema):
    id = fields.Int(required=True)

class EventUpdateSchema(Schema):
    event_status = fields.String(required=True, validate=validate.OneOf(choices=['DISABLE', 'ENABLE']))
    record_retain_day = fields.Integer()


class Record(inner_db.Model):
    ## 指定绑定的数据库和表名
    __bind_key__ = 'management'
    __tablename__ = 'request_record'
    id = inner_db.Column(inner_db.Integer , primary_key=True, nullable=False, autoincrement=True)
    url = inner_db.Column(inner_db.String(400), nullable=False)
    method = inner_db.Column(inner_db.String(10), nullable=False)
    body = inner_db.Column(inner_db.Text(16777216), nullable=False)
    create_time = inner_db.Column(inner_db.DateTime, default=datetime.datetime.now)
    account = inner_db.Column(inner_db.String(40), nullable=False)
    status = inner_db.Column(inner_db.SmallInteger, nullable=False)
    response = inner_db.Column(inner_db.Text(16777216), nullable=False)

    def get_data_info(self):
        return dict(id=self.id,
                    url=self.url,
                    method=self.method,
                    body=self.body,
                    create_time=self.create_time,
                    account=self.account,
                    status=self.status,
                    response=self.response
                    )

    def __str__(self):
        return "{}".format(self.get_data_info())

    def __repr__(self):
        return "{}".format(self.get_data_info())


